import dayjs from "dayjs";
import type { IEventCallbaskResult } from "./event_callback";
import type { IHilink } from "./hilink";
import { HuaweiTime, HuaweiTimestamp } from "./time";

export type { HuaweiTime } from "./time";
export type { IEventCallbaskResult } from "./event_callback";
export type { IProfileResult } from "./profile_result";
export type { IProfileService } from "./profile_service";
export type * from "./devinfo";
export type { IDeviceError } from "./code";
export type * from "./history_logs";

class Hilink implements IHilink {
  getDevCacheAll(devId: "0", json_body: string, cb: Function) {
    window.hilinkCb.getDevCacheAll = (res: string) => cb(parse(res));
    return window.hilink?.getDevCacheAll?.(devId, json_body, "hilinkCb.getDevCacheAll");
  }
  getDevInfo(devId: "0", sid: string, json_body: string, cb: Function) {
    window.hilinkCb.getDevInfo = (res: string) => cb(parse(res));
    return window.hilink?.getDevInfo?.(devId, sid, json_body, "hilinkCb.getDevInfo");
  }
  getDevInfoWithProdId(devId: "0", sid: string, json_body: string, cb: Function) {
    window.hilinkCb.getDevInfoWithProdId = (res: string) => cb(parse(res));
    return window.hilink?.getDevInfoWithProdId?.(devId, sid, json_body, "hilinkCb.getDevInfoWithProdId");
  }
  setDeviceInfo(devId: "0", json_body: string, cb: Function) {
    window.hilinkCb.setDeviceInfo = (res: string) => cb(parse(res));
    return window.hilink?.setDeviceInfo?.(devId, json_body, "hilinkCb.setDeviceInfo");
  }
  setMultipleDeviceInfo(devId: string, json_body: string, isParallel: string, isWaitAll: string, cb: Function) {
    window.hilinkCb.setMultipleDeviceInfo = (res: string) => cb(parse(res));
    return window.hilink?.setMultipleDeviceInfo?.(
      devId,
      json_body,
      isParallel,
      isWaitAll,
      "hilinkCb.setMultipleDeviceInfo"
    );
  }
  setDeviceInfoWithProdId(devId: string, json_body: string, cb: Function) {
    window.hilinkCb.setDeviceInfoWithProdId = (res: string) => cb(parse(res));
    return window.hilink?.setDeviceInfoWithProdId?.(devId, json_body, "hilinkCb.setDeviceInfoWithProdId");
  }
  setLongPressDeviceInfo(devId: string, json_body: string, cb: Function, isLongPressInProgress: boolean) {
    window.hilinkCb.setLongPressDeviceInfo = (res: string) => cb(parse(res));
    return window.hilink?.setLongPressDeviceInfo?.(
      devId,
      json_body,
      "hilinkCb.setLongPressDeviceInfo",
      isLongPressInProgress
    );
  }

  setDeviceServiceLoop(
    devId: string,
    serivceId: string,
    json_body: string,
    loopFlag: string,
    intervalTime: string,
    cb: Function
  ) {
    window.hilinkCb.setDeviceServiceLoop = (res: string) => cb(parse(res));
    return window.hilink?.setDeviceServiceLoop?.(
      devId,
      serivceId,
      json_body,
      loopFlag,
      intervalTime,
      "hilinkCb.setDeviceServiceLoop"
    );
  }
  setDeviceInfoWithoutCallback(devId: "0", json_body: string) {
    return window.hilink?.setDeviceInfoWithoutCallback?.(devId, json_body);
  }

  getDevHistory(
    devId: "0",
    pageNo: number,
    pageSize: number,
    startTime: string,
    endTime: string,
    sid: string,
    character: string,
    cb: Function
  ) {
    window.hilinkCb.getDevHistory = (res: string) => cb(parse(res));
    return window.hilink?.getDevHistory?.(
      devId,
      pageNo + "",
      pageSize + "",
      startTime,
      endTime,
      sid,
      character,
      "hilinkCb.getDevHistory"
    );
  }
  deleteDeviceHistory(devId: "0", serviceId: string, cursor: string | null, json_body: string) {
    return window.hilink?.deleteDeviceHistory?.(devId, serviceId, cursor, json_body);
  }
  registerInfraredHubDevice(json_body: string, cb: Function) {
    window.hilinkCb.registerInfraredHubDevice = (res: string) => cb(parse(res));
    return window.hilink?.registerInfraredHubDevice?.(json_body, "hilinkCb.registerInfraredHubDevice");
  }
  regiterInfraredHubDevice(json_body: string, cb: Function) {
    window.hilinkCb.regiterInfraredHubDevice = (res: string) => cb(parse(res));
    return window.hilink?.regiterInfraredHubDevice?.(json_body, "hilinkCb.regiterInfraredHubDevice");
  }
  deleteInfraredHubDevice(devId: "0", cb: Function) {
    window.hilinkCb.deleteInfraredHubDevice = (res: string) => cb(parse(res));
    return window.hilink?.deleteInfraredHubDevice?.(devId, "hilinkCb.deleteInfraredHubDevice");
  }
  postDeviceExtendData(json_body: string, cb: Function) {
    window.hilinkCb.postDeviceExtendData = (res: string) => cb(parse(res));
    return window.hilink?.postDeviceExtendData?.(json_body, "hilinkCb.postDeviceExtendData");
  }
  postDeviceExtendDataById(devId: string, cb: Function) {
    window.hilinkCb.postDeviceExtendDataById = (res: string) => cb(parse(res));
    return window.hilink?.postDeviceExtendDataById?.(devId, "hilinkCb.postDeviceExtendDataById");
  }
  getDeviceExtendData(type: string, cb: Function) {
    window.hilinkCb.getDeviceExtendData = (res: string) => cb(parse(res));
    return window.hilink?.getDeviceExtendData?.(type, "hilinkCb.getDeviceExtendData");
  }
  getDeviceExtendDataById(json_body: string, cb: Function) {
    window.hilinkCb.getDeviceExtendDataById = (res: string) => cb(parse(res));
    return window.hilink?.getDeviceExtendDataById?.(json_body, "hilinkCb.getDeviceExtendDataById");
  }
  deleteDeviceExtendData(type: string, cb: Function) {
    window.hilinkCb.deleteDeviceExtendData = (res: string) => cb(parse(res));
    return window.hilink?.deleteDeviceExtendData?.(type, "hilinkCb.deleteDeviceExtendData");
  }
  deleteDeviceExtendDataById(devId: string, type: string, cb: Function) {
    window.hilinkCb.deleteDeviceExtendDataById = (res: string) => cb(parse(res));
    return window.hilink?.deleteDeviceExtendDataById?.(devId, type, "hilinkCb.deleteDeviceExtendDataById");
  }
  reportDeviceProfileImpermanency(devId: "0", json_body: string, cb: Function) {
    window.hilinkCb.reportDeviceProfileImpermanency = (res: string) => cb(parse(res));
    return window.hilink?.reportDeviceProfileImpermanency?.(devId, json_body, "hilinkCb.reportDeviceProfileImpermanency");
  }
  deleteDevice(isAssociate: string, cb: Function) {
    window.hilinkCb.deleteDevice = (res: string) => cb(parse(res));
    return window.hilink?.deleteDevice?.(isAssociate, "hilinkCb.deleteDevice");
  }
  modifyDeviceName(deviceName: string, cb: Function) {
    window.hilinkCb.modifyDeviceName = (res: string) => cb(parse(res));
    return window.hilink?.modifyDeviceName?.(deviceName, "hilinkCb.modifyDeviceName");
  }
  modifyDeviceRoomName(chooseRoomString: string, cb: Function) {
    window.hilinkCb.modifyDeviceRoomName = (res: string) => cb(parse(res));
    return window.hilink?.modifyDeviceRoomName?.(chooseRoomString, "hilinkCb.modifyDeviceRoomName");
  }
  getDeviceInfo(cb: Function) {
    window.hilinkCb.getDeviceInfo = (res: string) => cb(parse(res));
    return window.hilink?.getDeviceInfo?.("hilinkCb.getDeviceInfo");
  }
  uploadObjectReq(file: string, additionInfo: string, cb: Function) {
    window.hilinkCb.uploadObjectReq = (res: string) => cb(parse(res));
    return window.hilink?.uploadObjectReq?.(file, additionInfo, "hilinkCb.uploadObjectReq");
  }
  downloadObjectReq(request: string, cb: Function) {
    window.hilinkCb.downloadObjectReq = (res: string) => cb(parse(res));
    return window.hilink?.downloadObjectReq?.(request, "hilinkCb.downloadObjectReq");
  }
  deleteObjectReq(request: string, cb: Function) {
    window.hilinkCb.deleteObjectReq = (res: string) => cb(parse(res));
    return window.hilink?.deleteObjectReq?.(request, "hilinkCb.deleteObjectReq");
  }
  getUserList(devId: string, cb: Function) {
    window.hilinkCb.getUserList = (res: string) => cb(parse(res));
    return window.hilink?.getUserList?.(devId, "hilinkCb.getUserList");
  }
  setUserList(userInfo: string, cb: Function) {
    window.hilinkCb.setUserList = (res: string) => cb(parse(res));
    return window.hilink?.setUserList?.(userInfo, "hilinkCb.setUserList");
  }
  updateUserList(userInfo: string, cb: Function) {
    window.hilinkCb.updateUserList = (res: string) => cb(parse(res));
    return window.hilink?.updateUserList?.(userInfo, "hilinkCb.updateUserList");
  }
  getDevStatisticsDataWithQuery(devId: string, request: string, cb: Function) {
    window.hilinkCb.getDevStatisticsDataWithQuery = (res: string) => cb(parse(res));
    return window.hilink?.getDevStatisticsDataWithQuery?.(devId, request, "hilinkCb.getDevStatisticsDataWithQuery");
  }
  getDevScalarData(devId: string, serviceId: string, characteristicName: string, cb: Function) {
    window.hilinkCb.getDevScalarData = (res: string) => cb(parse(res));
    return window.hilink?.getDevScalarData?.(devId, serviceId, characteristicName, "hilinkCb.getDevScalarData");
  }
  getDeviceProfile(cb: Function) {
    window.hilinkCb.getDeviceProfile = (res: string) => cb(parse(res));
    return window.hilink?.getDeviceProfile?.("hilinkCb.getDeviceProfile");
  }
  getBridgeSubclassRegisteredDevices(cb: Function) {
    window.hilinkCb.getBridgeSubclassRegisteredDevices = (res: string) => cb(parse(res));
    return window.hilink?.getBridgeSubclassRegisteredDevices?.("hilinkCb.getBridgeSubclassRegisteredDevices");
  }
  getSubDeviceUrl(productId: string, cb: Function) {
    window.hilinkCb.getSubDeviceUrl = (res: string) => cb(parse(res));
    return window.hilink?.getSubDeviceUrl?.(productId, "hilinkCb.getSubDeviceUrl");
  }
  getHilinkConnection(deviceId: string, cb: Function) {
    window.hilinkCb.getHilinkConnection = (res: string) => cb(parse(res));
    return window.hilink?.getHilinkConnection?.(deviceId, "hilinkCb.getHilinkConnection");
  }
  getDevInfoAll(deviceId: string, extendData: string, cb: Function) {
    window.hilinkCb.getDevInfoAll = (res: string) => cb(parse(res));
    return window.hilink?.getDevInfoAll?.(deviceId, extendData, "hilinkCb.getDevInfoAll");
  }
  getRoomList(cb: Function) {
    window.hilinkCb.getRoomList = (res: string) => cb(parse(res));
    return window.hilink?.getRoomList?.("hilinkCb.getRoomList");
  }
  getRules(condition: string, cb: Function) {
    window.hilinkCb.getRules = (res: string) => cb(parse(res));
    return window.hilink?.getRules?.(condition, "hilinkCb.getRules");
  }
  getSingleRule(ruleId: string, cb: Function) {
    window.hilinkCb.getSingleRule = (res: string) => cb(parse(res));
    return window.hilink?.getSingleRule?.(ruleId, "hilinkCb.getSingleRule");
  }
  createRule(rule: string, cb: Function) {
    window.hilinkCb.createRule = (res: string) => cb(parse(res));
    return window.hilink?.createRule?.(rule, "hilinkCb.createRule");
  }
  updateRule(rule: string, cb: Function) {
    window.hilinkCb.updateRule = (res: string) => cb(parse(res));
    return window.hilink?.updateRule?.(rule, "hilinkCb.updateRule");
  }
  deleteRules(ruleIds: string, cb: Function) {
    window.hilinkCb.deleteRules = (res: string) => cb(parse(res));
    return window.hilink?.deleteRules?.(ruleIds, "hilinkCb.deleteRules");
  }
  deleteSingleRule(ruleId: string, cb: Function) {
    window.hilinkCb.deleteSingleRule = (res: string) => cb(parse(res));
    return window.hilink?.deleteSingleRule?.(ruleId, "hilinkCb.deleteSingleRule");
  }
  controlRuleActive(ruleId: string, active: string, cb: Function) {
    window.hilinkCb.controlRuleActive = (res: string) => cb(parse(res));
    return window.hilink?.controlRuleActive?.(ruleId, active, "controlRuleActive");
  }
  getDeviceRuleEvents(cursor: string, pageSize: string, startTime: string, endTime: string, cb: Function) {
    window.hilinkCb.getDeviceRuleEvents = (res: string) => cb(parse(res));
    return window.hilink?.getDeviceRuleEvents?.(cursor, pageSize, startTime, endTime, "hilinkCb.getDeviceRuleEvents");
  }
  getAutoRuleSize(cb: Function) {
    window.hilinkCb.getAutoRuleSize = (res: string) => cb(parse(res));
    return window.hilink?.getAutoRuleSize?.("hilinkCb.getAutoRuleSize");
  }
  gotoVmall(suffix: string) {
    return window.hilink?.gotoVmall?.(suffix);
  }
  gotoNative(typeString: string, cb: Function) {
    window.hilinkCb.gotoNative = (res: string) => cb(parse(res));
    return window.hilink?.gotoNative?.(typeString, "hilinkCb.gotoNative");
  }
  showDelayInfoDialog() {
    return window.hilink?.showDelayInfoDialog?.();
  }
  jumpTo(
    uri:
      | "com.huawei.smarthome.timerPage"
      | "com.huawei.smarthome.deviceSettingActivity"
      | "com.huawei.smarthome.addSmartlockActivity"
      | "com.huawei.smarthome.deviceinfoactivity"
      | "com.huawei.smarthome.deviceNetinfoActivity",
    cb: Function
  ) {
    switch (uri) {
      case "com.huawei.smarthome.addSmartlockActivity":
        window.hilinkCb.jumpToAddSmartlockActivity = (res: string) => cb(parse(res));
        return window.hilink?.jumpTo?.(uri, "hilinkCb.jumpToAddSmartlockActivity");
      case "com.huawei.smarthome.deviceNetinfoActivity":
        window.hilinkCb.jumpToDeviceNetinfoActivity = (res: string) => cb(parse(res));
        return window.hilink?.jumpTo?.(uri, "hilinkCb.jumpToDeviceNetinfoActivity");
      case "com.huawei.smarthome.deviceSettingActivity":
        window.hilinkCb.jumpToDeviceSettingActivity = (res: string) => cb(parse(res));
        return window.hilink?.jumpTo?.(uri, "hilinkCb.jumpToDeviceSettingActivity");
      case "com.huawei.smarthome.deviceinfoactivity":
        window.hilinkCb.jumpToDeviceinfoactivity = (res: string) => cb(parse(res));
        return window.hilink?.jumpTo?.(uri, "hilinkCb.jumpToDeviceinfoactivity");
      case "com.huawei.smarthome.timerPage":
        window.hilinkCb.jumpToTimerPage = (res: string) => cb(parse(res));
        return window.hilink?.jumpTo?.(uri, "hilinkCb.jumpToTimerPage");
      default:
        break;
    }
  }

  jumpToOfflineHelpActivity(deviceId: string, hotline: string, cb: Function) {
    window.hilinkCb.jumpToOfflineHelpActivity = (res: string) => cb(parse(res));
    return window.hilink?.jumpToOfflineHelpActivity?.(deviceId, hotline, "hilinkCb.jumpToOfflineHelpActivity");
  }
  jumpToSelfOfflineHelpActivity(deviceId: string, message: string, cb: Function) {
    window.hilinkCb.jumpToSelfOfflineHelpActivity = (res: string) => cb(parse(res));
    return window.hilink?.jumpToSelfOfflineHelpActivity?.(deviceId, message, "hilinkCb.jumpToSelfOfflineHelpActivity");
  }
  startApp(packageName: string, cb: Function) {
    window.hilinkCb.startApp = (res: string) => cb(parse(res));
    return window.hilink?.startApp?.(packageName, "hilinkCb.startApp");
  }
  jumpToH5Device(listCount: number, cb: Function) {
    window.hilinkCb.jumpToH5Device = (res: string) => cb(parse(res));
    return window.hilink?.jumpToH5Device?.(listCount, "hilinkCb.jumpToH5Device");
  }
  modifyTitleBar(uri: boolean, argb: string, cb: Function) {
    window.hilinkCb.modifyTitleBar = (res: string) => cb(parse(res));
    return window.hilink?.modifyTitleBar?.(uri, argb, "hilinkCb.modifyTitleBar");
  }
  setTitleEnable(uri: boolean, cb: Function) {
    window.hilinkCb.setTitleEnable = (res: string) => cb(parse(res));
    return window.hilink?.setTitleEnable?.(uri, "hilinkCb.setTitleEnable");
  }
  setTitleVisible(uri: boolean, cb: Function) {
    window.hilinkCb.setTitleVisible = (res: string) => cb(parse(res));
    return window.hilink?.setTitleVisible?.(uri, "hilinkCb.setTitleVisible");
  }
  addMessage(json_body: string, cb: Function) {
    window.hilinkCb.addMessage = (res: string) => cb(parse(res));
    return window.hilink?.addMessage?.(json_body, "hilinkCb.addMessage");
  }
  removeMessage(json_body: string, cb: Function) {
    window.hilinkCb.removeMessage = (res: string) => cb(parse(res));
    return window.hilink?.removeMessage?.(json_body, "hilinkCb.removeMessage");
  }
  setMessageVisible(uri: boolean, cb: Function) {
    window.hilinkCb.setMessageVisible = (res: string) => cb(parse(res));
    return window.hilink?.setMessageVisible?.(uri, "hilinkCb.setMessageVisible");
  }
  changeTitleStyle(styleValue: number) {
    return window.hilink?.changeTitleStyle?.(styleValue);
  }
  showTitleBarIcons(json_body: string, buttonClick: string, isVisible: boolean, cb: Function) {
    window.hilinkCb.showTitleBarIcons = (res: string) => cb(parse(res));
    return window.hilink?.showTitleBarIcons?.(json_body, buttonClick, isVisible, "hilinkCb.showTitleBarIcons");
  }
  getStatusBarHeight(cb: Function) {
    window.hilinkCb.getStatusBarHeight = (res: string) => cb(parse(res));
    return window.hilink?.getStatusBarHeight?.("hilinkCb.getStatusBarHeight");
  }
  toast(time: number, body: string) {
    return window.hilink?.toast?.(time + "", body);
  }
  getPadLandscape() {
    return window.hilink?.getPadLandscape?.();
  }
  finishDeviceActivity() {
    return window.hilink?.finishDeviceActivity?.();
  }
  printLogDebug(isSaveLog: boolean, tag: string, msg: string) {
    return window.hilink?.printLogDebug?.(isSaveLog, tag, msg);
  }
  printLog(isSaveLog: boolean, tag: string, msg: string) {
    return window.hilink?.printLog?.(isSaveLog, tag, msg);
  }
  overrideBackPressed(enable: boolean, cb: Function) {
    window.hilinkCb.overrideBackPressed = (res: string) => cb(parse(res));
    return window.hilink?.overrideBackPressed?.(enable, "hilinkCb.overrideBackPressed");
  }
  overrideSettingClick(enable: boolean, cb: Function) {
    window.hilinkCb.overrideSettingClick = (res: string) => cb(parse(res));
    return window.hilink?.overrideSettingClick?.(enable, "hilinkCb.overrideSettingClick");
  }
  requestThirdPartConfig(json_body: string, cb: Function) {
    window.hilinkCb.requestThirdPartConfig = (res: string) => cb(parse(res));
    return window.hilink?.requestThirdPartConfig?.(json_body, "hilinkCb.requestThirdPartConfig");
  }
  setH5Info(key: string, value: string, cb: Function) {
    window.hilinkCb.setH5Info = (res: string) => cb(parse(res));
    return window.hilink?.setH5Info?.(key, value, "hilinkCb.setH5Info");
  }
  setStorageSync(key: string, value: string, cb: Function) {
    window.hilinkCb.setStorageSync = (res: string) => cb(parse(res));
    return window.hilink?.setStorageSync?.(key, value, "hilinkCb.setStorageSync");
  }
  getVersion(cb: Function) {
    window.hilinkCb.getVersion = (res: string) => cb(parse(res));
    return window.hilink?.getVersion?.("hilinkCb.getVersion");
  }
  refreshWebView() {
    return window.hilink?.refreshWebView?.();
  }
  verifyPassword(cb: Function) {
    window.hilinkCb.verifyPassword = (res: string) => cb(parse(res));
    return window.hilink?.verifyPassword?.("hilinkCb.verifyPassword");
  }
  getAppVersionCode(cb: Function) {
    window.hilinkCb.getAppVersionCode = (res: string) => cb(parse(res));
    return window.hilink?.getAppVersionCode?.("hilinkCb.getAppVersionCode");
  }
  getUserRole(cb: Function) {
    window.hilinkCb.getUserRole = (res: string) => cb(parse(res));
    return window.hilink?.getUserRole?.("hilinkCb.getUserRole");
  }
  reportH5SingleDevBiInfo(biKey: string, biH5Info: string) {
    return window.hilink?.reportH5SingleDevBiInfo?.(biKey, biH5Info);
  }
  takePhoto(fileName: string, cb: Function) {
    window.hilinkCb.takePhoto = (res: string) => cb(parse(res));
    return window.hilink?.takePhoto?.(fileName, "hilinkCb.takePhoto");
  }
  getAppLanguageSync() {
    return window.hilink?.getAppLanguageSync?.();
  }
  getDarkMode(cb: Function) {
    // IOS
    if (navigator.userAgent.toLocaleLowerCase().includes("like mac os x")) {
      window.hilinkCb.getDarkMode = cb;
      window.hilink?.getDarkMode?.("hilinkCb.getDarkMode");
      return;
    }
    // 安卓 其他
    const mode = window.hilink?.getDarkMode?.();
    cb(mode);
  }
  setGuideDialogEnable(enabled: boolean, cb: Function) {
    window.hilinkCb.setGuideDialogEnable = (res: string) => cb(parse(res));
    return window.hilink?.setGuideDialogEnable?.(enabled, "hilinkCb.setGuideDialogEnable");
  }
  isScreenSpreaded() {
    return window.hilink?.isScreenSpreaded?.();
  }
  createShortCut(deviceId: string, productId: string, deviceName: string, cb: Function = () => "") {
    window.hilinkCb.createShortCut = (res: string) => cb(parse(res));
    return window.hilink?.createShortCut?.(deviceId, productId, deviceName, "hilinkCb.createShortCut");
  }
  doHttpsRequest(jsonBody: string, cb: Function) {
    window.hilinkCb.doHttpsRequest = (res: string) => cb(parse(res));
    return window.hilink?.doHttpsRequest?.(jsonBody, "hilinkCb.doHttpsRequest");
  }
  refuseSharedDevice(cb: Function) {
    window.hilinkCb.refuseSharedDevice = (res: string) => cb(parse(res));
    return window.hilink?.refuseSharedDevice?.("hilinkCb.refuseSharedDevice");
  }
}

export function deviceEventCallback(cb: (res: IEventCallbaskResult) => void) {
  window.deviceEventCallback = (res: string) => cb(parse(res));
}

export function init() {
  if (!window.deviceEventCallback) {
    window.deviceEventCallback = () => {};
  }
  if (!window.onPause) {
    window.onPause = () => {};
  }
  if (!window.onResume) {
    window.onResume = () => {};
  }
  if (!window.onNetworkChanged) {
    window.onNetworkChanged = () => {};
  }
}

/**
 * 转换设备数据返回JSON结构
 * @param result
 * @returns
 */
export function parse(result: string) {
  // 去除object外多余的 "
  try {
    return JSON.parse(result);
  } catch (err) {
    console.warn(err);
    try {
      const _result = result
        .replace(/"{/g, "{")
        .replace(/}"/g, "}")
        .replace(/\\|\n|\r|\t|\f|\t/g, "");
      return JSON.parse(_result);
    } catch (e) {
      console.warn(e);
      return { errcode: 996, msg: result };
    }
  }
}

window.onDeviceEventCallback = () => {};

window.hilinkCb = {
  getDevCacheAll() {},
  getDevInfo() {},
  getDevInfoWithProdId() {},
  setDeviceInfo() {},
  setMultipleDeviceInfo() {},
  setDeviceInfoWithProdId() {},
  setLongPressDeviceInfo() {},
  setDeviceServiceLoop() {},
  registerInfraredHubDevice() {},
  regiterInfraredHubDevice() {},
  deleteInfraredHubDevice() {},
  postDeviceExtendData() {},
  postDeviceExtendDataById() {},
  getDeviceExtendData() {},
  getDeviceExtendDataById() {},
  deleteDeviceExtendData() {},
  deleteDeviceExtendDataById() {},
  reportDeviceProfileImpermanency() {},
  deleteDevice() {},
  modifyDeviceName() {},
  modifyDeviceRoomName() {},
  getDeviceInfo() {},
  uploadObjectReq() {},
  downloadObjectReq() {},
  deleteObjectReq() {},
  getUserList() {},
  setUserList() {},
  updateUserList() {},
  getDevStatisticsDataWithQuery() {},
  getDevScalarData() {},
  getDeviceProfile() {},
  getBridgeSubclassRegisteredDevices() {},
  getSubDeviceUrl() {},
  getHilinkConnection() {},
  getDevInfoAll() {},
  getRoomList() {},
  getRules() {},
  getSingleRule() {},
  createRule() {},
  updateRule() {},
  deleteRules() {},
  deleteSingleRule() {},
  controlRuleActive() {},
  getDeviceRuleEvents() {},
  getAutoRuleSize() {},
  gotoNative() {},
  jumpToTimerPage() {},
  jumpToDeviceinfoactivity() {},
  jumpToDeviceNetinfoActivity() {},
  jumpToAddSmartlockActivity() {},
  jumpToDeviceSettingActivity() {},
  jumpToOfflineHelpActivity() {},
  jumpToSelfOfflineHelpActivity() {},
  startApp() {},
  jumpToH5Device() {},
  modifyTitleBar() {},
  setTitleEnable() {},
  setTitleVisible() {},
  addMessage() {},
  removeMessage() {},
  setMessageVisible() {},
  showTitleBarIcons() {},
  getStatusBarHeight() {},

  overrideBackPressed() {},
  overrideSettingClick() {},
  requestThirdPartConfig() {},
  setH5Info() {},
  setStorageSync() {},
  getVersion() {},
  verifyPassword() {},
  getAppVersionCode() {},
  getUserRole() {},
  takePhoto() {},
  getDarkMode() {},
  setGuideDialogEnable() {},
  createShortCut() {},
  doHttpsRequest() {},
  refuseSharedDevice() {},
  getDevHistory() {},
};

export enum ThemeMode {
  // 1:正常模式 2:dark模式
  Default = 1,
  Dark = 2,
}

/**
 * 将华为世界时间格式转换为正常北京时间格式
 * 20150501T010203Z => 2015-05-01 01:02:03
 * @param t
 */
export function t2t(t: HuaweiTime): string {
  const matchArr = t.match(/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/);
  if (!matchArr) return "";
  const [_, y, mm, d, h, m, s] = matchArr;
  const yymm = `${y}-${mm}-${d} ${h}:${m}:${s}`;
  return dayjs(yymm).add(8, "hour").format("YYYY-MM-DD HH:mm:ss");
}

/**
 * 将华为时间戳格式转为正常时间戳格式
 * @param t
 * @returns number
 */
export function ts2t(t: HuaweiTimestamp) {
  return Number(t) || 0;
}

/**
 * 将北京正常时间转为华为时间
 * @param t
 * @returns
 */
export function t2ht(t: number | string | Date) {
  return dayjs(t).subtract(8, "hour").format("YYYYMMDDTHHmmss") + "Z";
}

const hilink = new Hilink();

/**
 * desc 获取手机信息，包含系统和屏幕
 * @param option
 */
export function getPhoneInfo() {
  let type = "android";
  let version = "10.0.0";
  let isPad = false;
  let isFold = false;
  let isPortrait = hilink.getPadLandscape() !== 0;
  let isDarkMode = false;
  let font;
  let changeScreen = false;
  let deviceType = "";
  try {
    let ua = navigator.userAgent.toLowerCase();
    let reg, matchInfo;
    if (ua.indexOf("like mac os x") > 0) {
      reg = /os [\d._]+/gi;
      type = "ios";
    } else if (ua.indexOf("android") > 0) {
      reg = /android [\d._]+/gi;
      type = "android";
    } else if (ua.indexOf("harmony") > 0) {
      reg = /harmony [\d._]+/gi;
      type = "harmony";
    }
    reg && (matchInfo = ua.match(reg));
    version = String(matchInfo)
      .replace(/[^0-9|_.]/gi, "")
      .replace(/_/gi, ".");
    let isUaPad =
      /(?:ipad|playbook)/.test(ua) || (["android", "harmony"].indexOf(type) !== -1 && !/(?:mobile)/.test(ua));
    let getPadLandscape = hilink.getPadLandscape() || 0;
    let isScreenSpreaded = hilink.isScreenSpreaded(); // 折叠屏展开
    isPad = isUaPad || getPadLandscape === 1 || getPadLandscape === 0;
    let browserWidth = document.documentElement.clientWidth;

    if (isPad) {
      if (isPortrait || getPadLandscape === 1) {
        // 10为rem换算单位，编写样式尺寸=（设计稿尺寸/10）rem
        // 平板竖屏设计稿宽度为800
        font = (browserWidth / 800) * 10;
        deviceType = "pad-S";
        changeScreen = true;
      } else {
        // 平板横屏设计稿宽度为422.752
        font = (browserWidth / 422.752) * 10;
        deviceType = "phone";
      }
    } else {
      if (window.screen.width > 500 || isScreenSpreaded) {
        // 折叠屏设计稿宽度为677
        font = (browserWidth / 677) * 10;
        // isFold = deviceType = "phone-FoldSreen";
        changeScreen = true;
      } else {
        //手机设计稿宽度为360
        font = (browserWidth / 360) * 10;
        deviceType = "phone";
      }
    }
    document.documentElement.style.fontSize = font + "px";
    hilink.getDarkMode((res: number) => {
      if (res === 2) {
        isDarkMode = true;
      }
    });
  } catch (err) {
    console.log("通过 navigator.userAgent 获取系统类型和版本报错,使用默认配置android 和 10");
  }

  return { type, version, isPad, isDarkMode, isFold, isPortrait, changeScreen, deviceType };
}

export default hilink;
